<?php
/**
 * class registry
 * so that singletons can be registered here
 * @author bruce.li
 * @static
 */
class Util_Reg
{
    static $REPO = array();

    /**
     * register the object
     * @param $key
     * @param $object
     * @param $overwrite default to true
     * @return void
     */
    static public function register($key, $object, $overwrite = true)
    {
        if (isset(self::$REPO[$key]) && !$overwrite) {
            throw new Exception('KEY: ' . $key . ' Already Exists');
        }

        self::$REPO[$key] = $object;

    }// end register


    /**
     * remove object by key
     * @param str $key
     * @return void
     */
    static public function remove($key)
    {
        if (isset(self::$REPO[$key])) {
            unset(self::$REPO[$key]);
        }

    }// end remove


    /**
     * remove by finding the right name...
     * @param str $find
     * @return unknown_type
     */
    static public function removeNs($find)
    {
        foreach (self::$REPO as $k => $v) {
            if (strpos($k, $find) === 0) {
                unset(self::$REPO[$k]);
            }
        }

    }// end removeNs


    /**
     * get object by key
     * @param str $key
     * @return void
     */
    static public function get($key)
    {
        if (isset(self::$REPO[$key])) {
            return self::$REPO[$key];
        }

        return false;

    }// end remove


    /**
     * clear all stored objects
     * @return void
     */
    static public function clearAll()
    {
        foreach (self::$REPO as $obj) {
            unset($obj);
        }

        self::$REPO = array();

    }// end clearAll


    /**
     * don't use it unless necessary
     * dumps all data
     * @return void
     */
    static public function dump()
    {
        echo '<pre>';
        var_dump(self::$REPO);
        echo '</pre>';

    }// end dump

}// end Util_Reg
?>